package com.zlx.mall.basic.common.exception;

import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * @author qingke
 **/
public class BaseException extends RuntimeException {
// TODO 2024/6/15 10:11 使用BaseRuntimeException
    private Integer code;

    public String message;

    public Throwable causeThrowable;

    public BaseException(String message) {
        super(message);
    }

    public BaseException(Integer code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }

    public BaseException(Integer code, Throwable causeThrowable) {
        super(causeThrowable);
        this.code = code;
        this.causeThrowable = causeThrowable;
    }

    public BaseException(Integer code, String message, Throwable causeThrowable) {
        super(message, causeThrowable);
        this.code = code;
        this.message = message;
        this.causeThrowable = causeThrowable;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    @Override
    public String getMessage() {
        if (StringUtils.hasText(this.message)) {
            return this.message;
        }
        if (this.causeThrowable != null) {
            return this.causeThrowable.getMessage();
        }
        return ObjectUtils.getDisplayString(this.message);
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Throwable getCauseThrowable() {
        return causeThrowable;
    }

    public void setCauseThrowable(Throwable causeThrowable) {
        this.causeThrowable = causeThrowable;
    }

    @Override
    public String toString() {
        return "code: " + getCode() + ", message: " + getMessage();
    }
}
